﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Net5ApiAndEfMySql.DtoModel;
using Net5CommonLib.SystemLib.Common;

namespace Net5ApiAndEfMySql.Repository
{
    public abstract class BaseRepository
    {

        /// <summary>
        /// soa_system_center 链接的属性
        /// </summary>
        public SoaSystemCenterDbContext SoaSystemCenterDbContext
        {
            //通过DiManager来获取di容器的SoaSystemCenter链接
            get { return DiManager.GetDiObjScoped<SoaSystemCenterDbContext>(); }
        }
        /// <summary>
        /// 公共的分页方法
        /// </summary>
        /// <typeparam name="P">查询结果的类型</typeparam>
        /// <param name="source">搜索条件</param>
        /// <param name="search">搜索model</param>
        /// <returns></returns>
        public PagerResult<P> Pager<P>(IQueryable<P> source, SearchModel search) where P : class, new()
        {
            int total = source.Count();
            PagerResult<P> result = new PagerResult<P>()
            {
                PageCount = (int)Math.Ceiling((decimal)total / search.limit),
                recordsFiltered = total,
                recordsTotal = total,
                data = source.Skip(search.start).Take(search.limit).ToList(),
                PageIndex = search.page 
            };
            return result;
        }

    }
}
